package us.kg.kotoriforest.springmvc.controller;

import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import us.kg.kotoriforest.springmvc.dao.EmployeeDao;
import us.kg.kotoriforest.springmvc.pojo.Employee;

import java.util.Collection;

/**
 * @author Kotori rotroutine@163.com
 * @since 2025/1/19
 */
@Controller
@AllArgsConstructor
public class EmployeeController {
    private final EmployeeDao employeeDao;

    @GetMapping("/employee")
    public ModelAndView getEmployees() {
        ModelAndView mav = new ModelAndView("employee_list");
        Collection<Employee> employeeList = employeeDao.getAll();
        mav.addObject("employeeList", employeeList);
        return mav;
    }

    @DeleteMapping("/employee/{id}")
    public String deleteEmployee(@PathVariable int id) {
        employeeDao.delete(id);
        return "redirect:/employee_list";
    }

    @PostMapping("/employee")
    public String addEmployee(Employee employee) {
        employeeDao.save(employee);
        return "redirect:/employee";
    }
}
